package org.longIt.springdata.controller;

import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;

import org.longIt.springdata.domain.Clazz;
import org.longIt.springdata.domain.Student;
import org.longIt.springdata.service.StudentService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class StudentController {
	
	@Resource
	private StudentService studentService;
	
	// 保存一些数据 
	@RequestMapping("/save")
	@ResponseBody
	public String save(){
		// 保存班级 
		Clazz clazz = new Clazz("java基础班");
		Clazz clazz1 = new Clazz("java就业班");
		List<Clazz> clazzs = new ArrayList<>();
		clazzs.add(clazz);
		clazzs.add(clazz1);
		studentService.saveAllClazz(clazzs);
		
		// 保存学生
		Student student1 = new Student("小明", "男", 20, "广州车陂", "15622266642", clazz1);
		Student student2 = new Student("阿毛", "男", 19, "广州车陂", "15622266633", clazz1);
		Student student3 = new Student("悟空", "男", 24, "广州棠下", "13433344433", clazz1);
		Student student4 = new Student("大龙", "男", 23, "广州上社", "15644466633", clazz);
		Student student5 = new Student("董小姐", "女", 24, "广州华景新城", "15644466683", clazz);
		List<Student> students = new ArrayList<>();
		students.add(student1);
		students.add(student2);
		students.add(student3);
		students.add(student4);
		students.add(student5);
		studentService.saveAllStus(students);
		
		return "保存数据成功！";
		
	}
		
	
	@RequestMapping("/index")
	public String index() {
		return "index";
	}
	
	// 根据班级名称查询学生
	@RequestMapping("/findStudentsByClazzName")
	public String findStudentsByClazzName(Model model,String clazzName) {
		List<Student> students = studentService.findStudentsByClazzName(clazzName);
		model.addAttribute("students", students);
		return "studentslist";
	}
	
	// 根据班级名称和性别查询学生
	@RequestMapping("/findStudentsByClazzNameAndSex")
	public String findStudentsByClazzNameAndSex(Model model,String clazzName,String sex) {
		List<Student> students = studentService.findStudentsByClazzNameAndSex(clazzName,sex);
		model.addAttribute("students", students);
		return "studentslist";
	}
	
	
	// 根据学生姓名删除学生
	@RequestMapping("/deleteByName")
	@ResponseBody
	public String deleteByName(Model model,String name) {
		System.out.println("name = " + name);
		studentService.deleteByName(name);
		
		return "删除学生成功";
	}

}
